package com.meituan.service.impl;

import com.meituan.mapper.OrderDetailMapper;
import com.meituan.mapper.OrderMapper;
import com.meituan.model.GoodsEntity;
import com.meituan.model.OrderEntity;
import com.meituan.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Override
    public List<OrderEntity> findAllByUserId(String userId) {
        return orderMapper.findAllByUserId(userId);
    }

    @Override
    public List<OrderEntity> findAllByShopId(String shopId) {
        return orderMapper.findAllByShopId(shopId);
    }

    @Override
    public Integer placeOrder(OrderEntity orderEntity, List<GoodsEntity> goodsEntityList) {
        Integer result = orderMapper.save(orderEntity);
        if(result==1){
            if(orderDetailMapper.save(String.valueOf(orderEntity.getOrderId()),goodsEntityList)>0){
                return orderEntity.getOrderId();
            }
        }
        return 0;
    }

    @Override
    public OrderEntity findById(String orderId) {
        return orderMapper.findById(orderId);
    }

    @Override
    public Integer updateIsDelete(String orderId) {
        return orderMapper.updateIsDelete(orderId);
    }


    @Override
    public Integer updateIsOver(String orderId) {
        return orderMapper.updateIsOver(orderId);
    }

    @Override
    public Integer updateIsPay( String orderId) {
        return orderMapper.updateIsPay(orderId);
    }
}
